package com.dai.zhenfujin.addresslist.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.Constants.EventConstants;
import com.dai.zhenfujin.base.BaseFragment;
import com.dai.zhenfujin.bean.AlumFansListBean;
import com.dai.zhenfujin.bean.MessageEvent;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.visitingcard.adapter.FansAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  粉丝列表
 */

public class FansPagerFragment extends BaseFragment {

    private ListView listView;
    private ImageView iv_no_msg;
    private int page = 1;
    private int number = 10;
    private List<AlumFansListBean.MsgListBean> msg_list = new ArrayList<>();
    private FansAdapter adapter;
    private SmartRefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, com.dai.zhenfujin.R.layout.fragment_list_fans,null);
        listView = view.findViewById(com.dai.zhenfujin.R.id.fans_listview);
        iv_no_msg = view.findViewById(com.dai.zhenfujin.R.id.iv_no_msg);
        iv_no_msg = view.findViewById(com.dai.zhenfujin.R.id.iv_no_msg);
        refreshLayout = view.findViewById(com.dai.zhenfujin.R.id.refreshLayout);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
      //  getFansList();
        adapter = new FansAdapter(mContext, msg_list);
        listView.setAdapter(adapter);
        onRefresh();
        initRefresh();
    }

    private void initRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getFansList(true);
                        refreshLayout.finishRefresh();
                        refreshLayout.resetNoMoreData();
                    }
                },1500);
            }
        });

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                getFansList(false);
            }
        });
    }

    private void onRefresh() {
        page = 1;
        getFansList(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event != null && event.getMessageKey() != null) {
            if (event.getMessageKey().equals(EventConstants.UPDATA_FANS_LIST)) {
                page = 1;
                getFansList(true);
            }
        }
    }

    private void getFansList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("page", page + "");
        params2.put("number", number + "");
        params2.put("type", 2 + "");
        params.put("req_bd",  params2);
        HttpClient.post(mContext, Constants.MSGLIST, params, new CallBack<AlumFansListBean>() {
            @Override
            public void onSuccess(AlumFansListBean resultBean) {
                SPUtils.put(getActivity(),"flag",false);
                if (isRefresh) {
                    msg_list.clear();
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }
                if (resultBean.getMsg_list().size() > 0) {
                    msg_list.addAll(resultBean.getMsg_list());
                    adapter.notifyDataSetChanged();
                    if (page==2)
                        iv_no_msg.setVisibility(View.GONE);
                }else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                   // if (page==2)
                        //iv_no_msg.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
